package com.zx.yixun.ui

import android.os.Bundle
import androidx.appcompat.widget.Toolbar
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.zx.yixun.R
import com.zx.yixun.data.AddPruductBean
import com.zx.yixun.data.GetPruductBean
import com.zx.yixun.data.ProductModel
import com.zx.yixun.pop.ChooseOnePop
import com.zx.yixun.util.DataStoreUtil
import com.zx.yixun.util.MyUtils
import com.zx.ymy.base.BaseActivity
import com.zx.ymy.net.NetWorkHelper
import com.zx.ymy.net.api.MyApi
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_setpeople.*
import java.util.*

class SetPeopleActivity :BaseActivity() {

    private lateinit var data:ProductModel
    private var processStepId="0"
    private var processId="0"
    private var productId="0"


    override fun getToolbar(): Toolbar? {
        return null
    }

    override fun getContentId(): Int {
        return R.layout.activity_setpeople
    }

    override fun onContentCreate(savedInstanceState: Bundle?) {
        getProducts(DataStoreUtil.getScreenId())
        tv_gxbz.setOnClickListener {
            ChooseOnePop(this,"选择工序步骤",MyUtils.covertBean(Gson().toJson(data.steps)), ChooseOnePop.OnChooseOneLinstener {
                it.let {
                    tv_gxbz.text=it.processStepName
                    tv_gx.text=it.processName
                    processId= it.processId!!
                    processStepId=it.processStepId!!
                }
            }).showPopupWindow()
        }

        tv_product.setOnClickListener {
            ChooseOnePop(this,"选择产品",MyUtils.covertBean(Gson().toJson(data.products)), ChooseOnePop.OnChooseOneLinstener {
                it.let {
                    tv_product.text=it.productNamae
                    productId=it.productId!!
                }
            }).showPopupWindow()
        }

        bt_done.setOnClickListener {
            if (processId == "0" ||processStepId=="0"||productId=="0"){
                ToastUtils.showShort("请选择完整！")
                return@setOnClickListener
            }
            addProducts(AddPruductBean(DataStoreUtil.getScreenId(),productId,processStepId,processId))
        }

        bt_cancel.setOnClickListener { finish() }
    }

    private fun getProducts(deviceId: String) {
        NetWorkHelper.instance().createApi(MyApi::class.java).getProducts(GetPruductBean(deviceId))
            .runRxLoading({
                it.let {
                    data= it!!
                }
            })
    }

    private fun addProducts(data:AddPruductBean) {
        NetWorkHelper.instance().createApi(MyApi::class.java).addPadProduct(data)
            .runRxLoading({
                ToastUtils.showShort("设置成功!")
                DataStoreUtil.savePeopleSet(true)
                ActivityUtils.startActivity(EsopActivity::class.java)
                finish()
            })
    }

}